package example.audio.track.controller;

import java.sql.Date;
import java.sql.Time;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import example.audio.track.Track;
import example.audio.track.service.TrackService;
import example.common.property.DatePropertyEditor;
import example.common.property.TimePropertyEditor;

@Controller
public class TrackController {
	@Autowired
	private TrackService trackService;
	
	@InitBinder
	public void initBinder(WebDataBinder dataBinder) {
		//dataBinder.setAllowedFields("id","title","filePath","playTime","added","updated");
		dataBinder.registerCustomEditor(Date.class, "playTime", new TimePropertyEditor());
		dataBinder.registerCustomEditor(Date.class, "added", new DatePropertyEditor());
		dataBinder.registerCustomEditor(Date.class, "updated", new DatePropertyEditor());
	}
	
	/*
	@RequestMapping(value="audio/track/trackList", method=RequestMethod.GET)
	public ModelAndView trackList() {
		return new ModelAndView("audio.track.trackList","tracks",trackService.getAllTracks());
	}
	
	@RequestMapping(value="audio/track/addTrackForm", method=RequestMethod.GET)
	public String addTrackForm(@ModelAttribute Track track) {
		return "audio.track.addTrackForm";
	}
	
	@RequestMapping(value="audio/track/addTrack", method=RequestMethod.POST)
	public ModelAndView addTrack(@ModelAttribute @Valid Track track, BindingResult result) {
		if (result.hasErrors()) return new ModelAndView("audio.track.addTrackForm");
		System.out.println(track);
		trackService.addTrack(track);
		return new ModelAndView("audio.track.addTrackSuccess","track",track);
	}
	
	@RequestMapping(value="audio/track/modifyTrackForm", method=RequestMethod.GET)
	public ModelAndView modifyTrackForm(@ModelAttribute Track track, @RequestParam String id) {
		return new ModelAndView("audio.track.modifyTrackForm","track",trackService.getTrackById(Integer.valueOf(id)));
	}
	*/
	@RequestMapping(value="audio/track/trackList", method=RequestMethod.GET)
	public String trackList(ModelMap modelMap) {
		modelMap.addAttribute("tracks", trackService.getAllTracks());
		return "audio.track.trackList";
	}
	
	@RequestMapping(value="audio/track/addTrackForm", method=RequestMethod.GET)
	public String addTrackForm(ModelMap modelMap) throws ParseException {
		modelMap.addAttribute(new Track());
		return "audio.track.addTrackForm";
	}
	
	@RequestMapping(value="audio/track/addTrack", method=RequestMethod.POST)
	public String addTrack(@ModelAttribute @Valid Track track, BindingResult result) {
		if (result.hasErrors()) return "audio.track.addTrackForm";
		trackService.addTrack(track);
		return "redirect:/audio/track/addTrackSuccess.htm";
	}
	
	@RequestMapping(value="/audio/track/addTrackSuccess", method=RequestMethod.GET)
	public String addTrackSuccess() {
		return "audio.track.addTrackSuccess";
	}
	
	@RequestMapping(value="audio/track/modifyTrackForm", method=RequestMethod.GET)
	public String modifyTrackForm(@RequestParam String id, ModelMap modelMap) {
		modelMap.addAttribute("track", trackService.getTrackById(Integer.valueOf(id)));
		return "audio.track.modifyTrackForm";
	}
	
	@RequestMapping(value="audio/track/modifyTrack", method=RequestMethod.POST)
	public String modifyTrack(@ModelAttribute @Valid Track track, BindingResult result) {
		if (result.hasErrors()) return "audio.track.modifyTrackForm";
		trackService.updateTrack(track);
		return "redirect:/audio/track/modifyTrackSuccess.htm";
	}
	
	@RequestMapping(value="/audio/track/modifyTrackSuccess", method=RequestMethod.GET)
	public String modifyTrackSuccess() {
		return "audio.track.modifyTrackSuccess";
	}
	
	@RequestMapping(value="audio/track/conditionalTrackSearch", method=RequestMethod.GET)
	public String conditionalTrackSearch(@RequestParam(required=false) String length, ModelMap modelMap) {
		Map<String,Object> params = new HashMap<String,Object>();
		if (length != null && !"".equals(length)) params.put("playTime", Time.valueOf(length));
		modelMap.addAttribute("tracks", trackService.getConditionalTracks(params));
		return "audio.track.trackList";
	}
}
